<template>
  <el-form label-width="120px">
    <el-form-item label="SKU名称">
      <el-input
        placeholder="请你输入SKU名称"
        v-model="skuParams.skuName"
      ></el-input>
    </el-form-item>
    <el-form-item label="SKU价格(元)">
      <el-input
        placeholder="请你输入SKU价格"
        type="number"
        v-model="skuParams.price"
      ></el-input>
    </el-form-item>
    <el-form-item label="SKU重量(克)">
      <el-input
        placeholder="请你输入SKU重量"
        type="number"
        v-model="skuParams.weight"
      ></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input
        placeholder="请你输入SKU描述"
        type="textarea"
        v-model="skuParams.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true">
        <el-form-item
          v-for="(attr, index) in attrArr"
          :key="attr.id"
          :label="attr.attrName"
        >
          <el-select v-model="attr.attrIdAndValueId">
            <el-option
              :value="`${attr.id}:${attrValue.id}`"
              :label="attrValue.valueName"
              v-for="(attrValue, index) in attr.attrValueList"
              :key="attrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true">
        <el-form-item
          :label="sale.saleAttrName"
          v-for="(sale, index) in saleArr"
          :key="sale.id"
        >
          <el-select v-model="sale.data">
            <el-option
              :label="saleValue.saleAttrValueName"
              :value="`${sale.id}:${saleValue.id}`"
              :key="saleValue.id"
              v-for="(saleValue, index) in sale.spuSaleAttrValueList"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border stripe :data="imgArr">
        <el-table-column
          type="selection"
          align="center"
          width="80px"
        ></el-table-column>
        <el-table-column label="图片">
          <template #="{ row, $index }">
            <img :src="row.imgUrl" alt="" style="width: 100px" />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"> </el-table-column>
        <el-table-column label="操作">
          <template #="{ row, $index }">
            <el-button type="primary" size="small" @click=""
              >默认图片</el-button
            >
            <el-button type="danger" size="small" @click="">设置默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="default">保存</el-button>
      <el-button
        size="default"
        @click="$emits('changeScene', { scene: 0, flag: 'update' })"
        >取消</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
// 获取到接口
import {
  reqSkuImageList,
  reqAllAttrList,
  reqSpuSaleAttr,
} from "@/api/product/spu";
// 存储全部的平台属性
const attrArr = ref<any>([]);
// 存储销售属性
const saleArr = ref<any>([]);
// 存储照片墙
const imgArr = ref<any>([]);
// 添加sku需要携带的参数
const skuParams = reactive<any>({
  // 三级分类的Id
  category3Id: "",
  // 已有Spu的Id
  spuId: "",
  // 品牌的ID
  tmId: "",
  // sku名字
  skuName: "",
  // 商品的价格
  price: "",
  // 商品的重量
  weight: "",
  // 商品的描述
  skuDesc: "",
  // 商品的库存
  skuDefaultImg: "",
  // 平台属性的收集
  skuAttrValueList: [],
  // 销售属性的收集
  skuSaleAttrValueList: [],
});
// 发送请求的函数
const initAddSku = async (c1Id: any, c2Id: any, spu: any) => {
  // 收集三级分类的ID,spu,品牌的ID
  skuParams.category3Id = spu.category3Id;
  //
  skuParams.spuId = spu.id;
  //
  skuParams.tmId = spu.tmId;
  // 发送获取全部的函数的请求
  let result = await reqAllAttrList(c1Id, c2Id, spu.category3Id);
  // 存储全部的
  attrArr.value = result;
  // 获取全部的销售属性
  let result1 = await reqSpuSaleAttr(spu.id);
  // 存储全部的销售的属性
  saleArr.value = result1;
  // 获取全部的照片
  let result2 = await reqSkuImageList(spu.id);
  // 存储全部的照片
  imgArr.value = result2;
};
const $emits = defineEmits(["changeScene"]);
// 把请求函数暴露出去
defineExpose({ initAddSku });
</script>

<style scoped></style>
